package web.factory;


import com.fz.us.base.util.LogUtil;
import com.joint.base.entity.quartz.ScheduleJob;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 任务工厂类,非同步
 *
 * User: liyd
 * Date: 14-1-3
 * Time: 上午10:11
 */
@DisallowConcurrentExecution
public class JobFactory implements Job {

    /* 日志对象 */
    private static final Logger LOG = LoggerFactory.getLogger(JobFactory.class);

    public void execute(JobExecutionContext context) throws JobExecutionException {

        LOG.info("JobFactory execute");

        ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
        try {
            Thread.sleep(10000);
            Class clazz = Class.forName(scheduleJob.getTargetObject());
            Method method = clazz.getMethod(scheduleJob.getTargetMethod(), String.class);
            method.invoke(clazz.newInstance(), scheduleJob.getJobId());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            LogUtil.error(scheduleJob.getTargetObject()+"找不到");
        }catch (InterruptedException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            LogUtil.error(scheduleJob.getTargetMethod() + "找不到");
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}
